
import router from '@/router'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import store from '@/store'

// 前置守卫
const whiteList = ['/login', '/404']
router.beforeEach((to, from, next) => {
  nprogress.start() // 开启进度条
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
      nprogress.done()
    } else {
      next() // 放行
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) { // includes判断有没有特定的元素
      next()
    } else {
      next('/login') // 中转到登录页
      nprogress.done()
    }
  }
})

// 后置首位
router.afterEach(() => {
  nprogress.done()
})
